package com.bluerox.updating;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

/** 
* 取得MD5编码类<br><br>
* 
* Example:<br><br>
* 
* String filename = "/home/lijianbo/test/Race/start.bin";<br>
* String hashType = "MD5";<br><br>
* 
* MD5.getHash(filename, hashType);
* 
* @author 李建波 
*/
public class MD5 {
	  
	    public static char[] hexChar = {'0', '1', '2', '3',  
	                                   '4', '5', '6', '7',  
	                                   '8', '9', 'a', 'b',  
	                                   'c', 'd', 'e', 'f'};
	    
	    /**
	     * 可用此类得到目标文件的MD5
	     * @param args
	     * @throws Exception
	     */
	    public static void main(String[] args) throws  
	            Exception {  
	    	//获得文件的MD5码不会因为文件名的不同而不同，MD5码是对文件里的内容进行处理的，内容相同的文件，即使文件名不同，但是它们的MD5码是相同的
	        String fileName = "/var/lib/tomcat6/webapps/ROOT/race_test/race1.0.0/bmw_data.bin.zip";//  9ec3e92933a0c0fb28e40f6a313ad5d0
	        String hashType = "MD5";  
	        System.out.println(hashType + " == " +  
	                           getHash(fileName, hashType));  
	        hashType = "SHA1";  
	        System.out.println(hashType + " == " +  
	                           getHash(fileName, hashType));  
	        hashType = "SHA-256";  
	        System.out.println(hashType + " == " +  
	                           getHash(fileName, hashType));  
	        hashType = "SHA-384";  
	        System.out.println(hashType + " == " +  
	                           getHash(fileName, hashType));  
	        hashType = "SHA-512";  
	        System.out.println(hashType + " == " +  
	                           getHash(fileName, hashType));  
	  
	    }  
	    
	    /**
	     * 根据编码类型得到目标文件的编码<br><br>
	     * Example:<br><br>
	     *  
		 * String filename = "/home/lijianbo/test/Race/start.bin";<br>
		 * String hashType = "MD5";<br><br>
		 * 
		 * MD5.getHash(filename, hashType);<br>
	     * 
	     * @param fileName 目标文件名
	     * @param hashType 类型 "MD5", "SHA1", "SHA-256", "SHA-384", "SHA-512"
	     * @return 目标文件MD5编码
	     * @throws Exception
	     */
	    public static String getHash(String fileName, String hashType) throws  
	            Exception {  
	        InputStream fis;  
	        fis = new FileInputStream(fileName);  
	        byte[] buffer = new byte[1024];  
	        MessageDigest md5 = MessageDigest.getInstance(hashType);  
	        int numRead = 0;  
	        while ((numRead = fis.read(buffer)) > 0) {  
	            md5.update(buffer, 0, numRead);  
	        }  
	        fis.close();  
	        return toHexString(md5.digest());  
	    }  
	    
	    /**
	     * 
	     * @param b
	     * @return 相应的编码
	     */
	    public static String toHexString(byte[] b) {  
	        StringBuilder sb = new StringBuilder(b.length * 2);  
	        for (int i = 0; i < b.length; i++) {  
	            sb.append(hexChar[(b[i] & 0xf0) >>> 4]);  
	            sb.append(hexChar[b[i] & 0x0f]);  
	        }  
	        return sb.toString();  
	    }  
}
